﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Quản_Lý_Thư_Viện
{
    public partial class frmNhapSach : Form
    {
        public frmNhapSach()
        {
            InitializeComponent();
            
        }
        SachControl control = new SachControl();
        CTPMcontrol ctpmctrl = new CTPMcontrol();
        CT_PTScontrol tsctrol = new CT_PTScontrol();
        //chuthich
        public static string timsach;
        //
        bool kt = false;
        bool bInserted = false;

        private void frmNhapSach_Load(object sender, EventArgs e)
        {
            DausachControl ds = new DausachControl();
            DataTable dt = ds.LayDSDausach();
            cboDauSach.DataSource = dt;
            cboDauSach.ValueMember = "MADAUSACH";
            cboDauSach.DisplayMember = "TENSACH";
            dgvSach.DataSource = control.LayDSsach();
        }
        private void laythongtinsach(ref SachInfo info)
        {
            
            info.Masach = txtMaSach.Text;
            info.Tinhtrang = cboTinhTrang.SelectedItem.ToString(); ;
            info.Madausach = cboDauSach.SelectedValue.ToString();
        }

        private void btnNhapDauSach_Click(object sender, EventArgs e)
        {
            frmNhapDauSach nhapdausach = new frmNhapDauSach();
            nhapdausach.ShowDialog();
        }

        private void btnNhapTheLoai_Click(object sender, EventArgs e)
        {
            frmTheLoaiSach nhaptheloai = new frmTheLoaiSach();
            nhaptheloai.ShowDialog();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            SachInfo sinfo = new SachInfo();
            if (bInserted)
            {
                
                    laythongtinsach(ref sinfo);
                    if (kiemtra(txtMaSach.Text.Trim()) == true)
                    {
                        MessageBox.Show("Trùng Mã Sách");
                    }
                    else
                    {
                        control.insertsach(sinfo);
                        MessageBox.Show("Thêm Sách Thành Công");
                        dgvSach.DataSource = control.LayDSsach();
                        bInserted = false;
                        ShowControl(false);
                        btnSua.Enabled = true;
                        btnThem.Enabled = true;
                        
                    }
                
            }
            else
            {              
                CT_PTSinfo tsinfo = new CT_PTSinfo();
                tsinfo.Masach = txtMaSach.Text;
                laythongtinsach(ref sinfo);
                control.updatesach(sinfo);
                tsctrol.updateCTPTS(tsinfo);
                MessageBox.Show("Sửa Sách Thành Công");
                frmNhapSach_Load(sender, e);
                ShowControl(false);
                btnSua.Enabled = true;
                btnThem.Enabled = true;
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            SachInfo sinfo = new SachInfo();
            CT_PTSinfo tsinfo = new CT_PTSinfo();
            tsinfo.Masach = txtMaSach.Text;
            tsctrol.deleteCTPTS(tsinfo);
            laythongtinsach(ref sinfo);
            CTPMinfo pminfo = new CTPMinfo();
            pminfo.Masach = dgvSach.CurrentRow.Cells["MASACH"].Value.ToString();
            ctpmctrl.deletectpm(pminfo);
            control.deletesach(sinfo);
            MessageBox.Show("Xóa Sách Thành Công");
            frmNhapSach_Load(sender, e);
        }
        DataTable dt = new DataTable();
        private void taodatatable()
        {
            dt.Columns.Add("MASACH");
            dt.Columns.Add("MADAUSACH");
            dt.Columns.Add("TINHTRANG");
        }
        public void deletetempsach()
        {

            SachInfo s = new SachInfo();
            control.deletetemp(s);
           
        }
        private void btnSua_Click(object sender, EventArgs e)
        {
            bInserted = false;
            ShowControl(true);
            btnSua.Enabled = false;
            btnThem.Enabled = false;
            
        }

        private void dgvSach_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvSach.RowCount > 0)
            {
                txtMaSach.Text = dgvSach.CurrentRow.Cells["MASACH"].Value.ToString();
                cboTinhTrang.SelectedItem = dgvSach.CurrentRow.Cells["TINHTRANG"].Value.ToString();
                cboDauSach.SelectedValue = dgvSach.CurrentRow.Cells["MADAUSACH"].Value.ToString();
            }
        }
        private Boolean kiemtra(string ma)
        {

            for (int j = 0; j < dgvSach.RowCount ; j++)
            {
                if (ma == dgvSach.Rows[j].Cells[0].Value.ToString())
                {
                    kt = true;
                    break;
                }
                else
                    kt = false;
            }
            return kt;
        }
        private void ShowControl(bool bShow)
        {
            txtMaSach.Enabled = bInserted;
            cboDauSach.Enabled = bShow;
            cboTinhTrang.Enabled = bShow;
            btnSave.Enabled = bShow;
        }
        private void Clear()
        {
            txtMaSach.Text = "";

        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            bInserted = true;
            Clear();
            ShowControl(true);
            btnSua.Enabled = false;
            btnThem.Enabled = false;
            int i = dgvSach.Rows.Count;
            txtMaSach.Text = "Sach" + i + "";
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            if (dgvSach.RowCount > 0)
            {
                txtMaSach.Text = dgvSach.CurrentRow.Cells["MASACH"].Value.ToString();
                cboTinhTrang.SelectedItem = dgvSach.CurrentRow.Cells["TINHTRANG"].Value.ToString();
                cboDauSach.SelectedValue = dgvSach.CurrentRow.Cells["MADAUSACH"].Value.ToString();
            }
            bInserted = false;
            ShowControl(false);
            btnSua.Enabled = true;
            btnThem.Enabled = true;
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Escape))
            {
                this.Close();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }


        private void tbxtimkiem_TextChanged(object sender, EventArgs e)
        {
            timsach = tbxtimkiem.Text;
            dgvSach.DataSource = control.tims();
        }      
        //
    }
}
